package com.koron.css2.systemManage.servlet;

import com.koron.css2.ApplicationConfig;
import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.systemManage.bean.OperationTreeBean;
import com.koron.css2.systemManage.service.ServiceFactory;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import com.koron.util.TreeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

public class OrgOperationList implements ServerInterface {
	
	private static Logger log = LogManager.getLogger(OrgOperationList.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		MessageBean<List> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", List.class);
		try {
			String orgCode = JsonUtils.objectToPojo(req.getData(), String.class);
			if(StringUtils.isEmpty(orgCode)) {
				msg.setCode(Constant.MESSAGE_INT_FAIL);
				msg.setDescription("组织编码不能为空");
				return msg;
			}
			List<OperationTreeBean> operationTree = ServiceFactory.getOperationService().listOPByOrg(ApplicationConfig.getAppCustomerAppid(),userInfo, orgCode, true);
			operationTree = TreeUtils.list2TreeList(operationTree, "opCode", "parentOpCode", "children");
			msg.setData(operationTree);
		} catch (Exception e) {
			log.error("查询角色菜单出错：" + e);
			msg.setCode(Constant.MESSAGE_INT_FAIL);
			msg.setDescription("查询角色菜单出错");
		}
		return msg;
	}
}
